package com.snail.readyvr.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.adapter.DownloadingAdapter;
import com.snail.readyvr.util.LineItemDecoration;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import party.loveit.download.core.DLInfo;
import party.loveit.download.core.DLListenerMgr;
import party.loveit.download.core.DLManager;
import party.loveit.download.listener.IDListener;
import party.loveit.swipemenu.core.SwipeMenuRecyclerView;

/**
 * Created by MRKING on 2017/1/9.
 */

public class DownloadingFragment extends BaseFragment implements IDListener {

    @BindView(R.id.download_recycler_view)
    SwipeMenuRecyclerView mDownloadRecyclerView;
    @BindView(R.id.no_download_tip)
    TextView mNoDownloadTip;

    private boolean hasCallData = false;

    private DownloadingAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_download;
    }

    @Override
    protected void init() {
        DLListenerMgr.getInstance().register(this);
        mNoDownloadTip.setText(R.string.no_video_downloading_tip);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mDownloadRecyclerView.setLayoutManager(manager);
        mDownloadRecyclerView.addItemDecoration(new LineItemDecoration(getActivity(), LineItemDecoration.VERTICAL_LIST));
        mAdapter = new DownloadingAdapter(getActivity(), R.layout.item_downloading, mDownloadRecyclerView);
        mDownloadRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && !hasCallData) {
            hasCallData = true;
            getData();
        }
    }

    private void getData() {
        List<DLInfo> infos = DLManager.getInstance().getAllNoCompleteDLInfo();
        if(infos != null && infos.size() > 0)
            mAdapter.addDatas(infos);
        else
            mNoDownloadTip.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        DLListenerMgr.getInstance().unregister(this);
    }

    @Override
    public void onPrepare(String baseUrl) {
        if (!hasCallData)
            return;
    }

    @Override
    public void onStart(String fileName, String baseUrl, long fileLength) {
        if (!hasCallData)
            return;
        mAdapter.itemChangedState(baseUrl);
    }

    @Override
    public void onProgress(String baseUrl, String speed, long progress) {
        if (!hasCallData)
            return;
        mAdapter.changeProgress(baseUrl);
    }

    @Override
    public void onStop(String baseUrl, long progress) {
        if (!hasCallData)
            return;
        mAdapter.itemChangedState(baseUrl);
    }

    @Override
    public void onFinish(String baseUrl, File file) {
        if (!hasCallData)
            return;
        mAdapter.deleteItem(baseUrl);
    }

    @Override
    public void onError(String baseUrl, int status, String error) {
        if (!hasCallData)
            return;
        mAdapter.itemChangedState(baseUrl);
    }
}
